// pages/authorization/authorization.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //判断小程序的API，回调，参数，组件等是否在当前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    isHide: true
  },
  getUserProfile: function () {
    var that = this;
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        var avatarUrl = res.userInfo.avatarUrl;
        var nickName = res.userInfo.nickName;
        var gender = res.userInfo.gender;
        wx.login({
          success: function (resData) {
            var pid = wx.getStorageSync('pid')

            var datas = {
              headimage: avatarUrl,
              nickname: nickName,
              code: resData.code,
              pid:pid
            };
           
            // return false;
            app.wxRequest('POST', "wxapp/getUserOpenId", datas, (res) => {
              var code = res.code;
              if(code == 1){
                var param={
                  openid:res.data.openid,
                  headimage:avatarUrl,
                  nickname:nickName
                }
                wx.setStorageSync('uid', param);
                wx.navigateBack({
                  delta: 0,
                })
              }else{
                wx.showToast({
                  title: '操作失败,请重试',
                  icon:"none",
                  duration:1500
                })
              }
              // var userinfo = res.data;
              console.log(res)
              // var uid = userinfo.uid;
              // wx.setStorageSync("userInfo", userinfo);
              // wx.setStorageSync("uid", uid);
              // wx.navigateBack({

              // })
            })
          },
          fail: res => {
            console.log("获取用户信息失败", res)
          }
        })
      }
    })
  }
})